#include "aisinoutils.h"
#include <stdio.h>
#include <string.h>


AisinoException::AisinoException(const std::string& msg)
{
    this->msg = msg;
}

AisinoException::~AisinoException()
{

}

const char* AisinoException::what() const noexcept
{
    return this->msg.c_str();
}




AisinoUtils::~AisinoUtils()
{

}

AisinoUtils::AisinoUtils()
{

}

std::string AisinoUtils::StringIgnoreSign(std::string text)
{
    std::string signs[] = {".", " ","_","	", ""};
    std::string result = text;

    for(int i = 0 ; 0 != signs[i].compare("");i++)
    {
        result = AisinoUtils::replaceStr(result,signs[i].c_str(),"");
    }
    return result;
}

std::string AisinoUtils::replaceStr(std::string text,std::string oldtext,std::string newtext)
{
    std::string result = text;
    for(std::string::size_type pos = result.find(oldtext); pos != std::string::npos; pos = result.find(oldtext))
    {
        result = result.replace(pos,oldtext.length(),newtext);
    }

    return result;
}

std::string AisinoUtils::getInnerText(std::string text,std::string prefix,std::string suffix)
{
    std::string::size_type findPre = text.find(prefix);
    std::string::size_type findSuf = text.find(suffix);
    std::string::size_type preLength = prefix.length();
    std::string result = text.substr(findPre + preLength,findSuf - preLength - findPre);
    return result;
}

bool AisinoUtils::writeLine(std::ostream& ostream,std::string text)
{
    try{
        ostream.write(text.c_str(),text.length());
        ostream.write("\n",strlen("\n"));
        return true;
    }catch (std::ios_base::failure& fail) {
        printf("writeLine failed:%s\n",fail.what());
        return false;
    }
}

bool AisinoUtils::isStringWithTag(const std::string& text,const std::string& tag)
{
    if(std::string::npos == text.find(tag))
        return false;
    return true;
}

std::string AisinoUtils::getStringBehindTag(const std::string& text,const std::string& tag) throw(AisinoException&)
{
    if(!AisinoUtils::isStringWithTag(text,tag))
    {
        std::string errorMsg = text + " not have tag:" + tag;
        throw AisinoException(errorMsg);
    }

    std::string::size_type pos = text.find(tag);
    pos += tag.length();
    return text.substr(pos);
}

std::string AisinoUtils::getStringFrontTag(const std::string& text,const std::string& tag) throw(AisinoException&)
{
    if(!AisinoUtils::isStringWithTag(text,tag))
    {
        std::string errorMsg = text + " not have tag:" + tag;
        throw AisinoException(errorMsg);
    }
    std::string::size_type pos = text.find(tag);
    return text.substr(0,pos);
}

std::string AisinoUtils::stringInsertLineFrontTag(const std::string& text,const std::string& tag)
{
    std::string result = text;
    for(std::string::size_type pos= result.find(tag) ; std::string::npos != pos ; pos = result.find(tag,pos + 2))
    {
        result.insert(pos,"\n");
    }
    return result;
}
